<?php
/**
 * User: 苏翔
 * Date: 2024/8/11 19:01
 */

namespace App\Service;

use App\Amqp\Producers\FundBalanceProducer;
use App\Amqp\Producers\FundProducer;
use App\Amqp\Producers\UserBalanceProducer;
use App\Dao\FundCacheDao;
use App\Dao\HscoinBalanceDao;
use App\Dao\HscoinBillDao;
use App\Dao\HscoinExchangeDao;
use App\Dao\PriceLogDao;
use App\FundConf;
use App\HscoinConf;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Redis\Redis;
use Hyperf\Amqp\Producer;

class HscoinExchangeService extends BaseService
{

    #[Inject]
    private HscoinExchangeDao $dao;
    #[Inject]
    private Redis $redis;

    #[Inject]
    private HscoinBalanceDao $balanceDao;

    #[Inject]
    private HscoinBillDao $hscoinBillDao;
    #[Inject]
    private producer $producer;
    #[Inject]
    protected HscoinStatsService $statsService;
    #[Inject]
    protected PriceLogDao $priceLogDao;
    function exchange($uid, $hscoin_amount, $price, $fee_rate = 0)
    {

        $fee = 0;
        if ($fee_rate) {
            $fee = $hscoin_amount * $fee_rate * $price;
        }
        $rt = false;
        $result_amount = $hscoin_amount * $price;
        $link_amount = $hscoin_amount * $price;
        $log = compact('uid', 'hscoin_amount', 'price', 'fee_rate', 'fee', 'result_amount');
        $this->dao->getConnection()->transaction(function () use (&$rt, &$log, $uid, $hscoin_amount, $link_amount) {
            if (!$this->checkAmount($uid, $hscoin_amount)) {
                return $rt = false;
            }
            $id = $this->dao->insertGetId($log);
            $log['id'] = $id;
            $log['link_amount'] = $link_amount;
            $rs = $this->addBill($log);
            $rt = true;
        });

        //
        if ($rt && $rt) {

            $amount = $this->statsService->getBalance();
            $this->priceLogDao->changeByHscoin($amount, 0);
            $this->mqUser($log);
            $this->mqFund($log);
        }

        return $rt;
    }

    function addBill($log)
    {
        $bill = [
            'uid' => $log['uid'],
            'category' => HscoinConf::CATEGORY_EXPENSE_EXCHANGE,
            'link_id' => $log['id'],
            'link_amount' => $log['link_amount'],
            'amount' => $log['hscoin_amount'],
            'mark' => '兑换',
            'status' => FundConf::STATUS_ENABLED,
            'add_time' => time(),
        ];
        return $this->hscoinBillDao->add($bill['category'], $bill['link_id'], $bill['link_amount'], $bill['amount'], $bill['uid'], $bill['mark']);
    }


    function checkAmount($uid, $amount)
    {
        $balance = (float)$this->balanceDao->getBalance($uid);
        return $balance >= $amount;
    }

    function mqUser($data)
    {

        return $this->producer->produce(new UserBalanceProducer($data), true, 5);

    }

    function mqFund($data)
    {

        return $this->producer->produce(new FundProducer($data), true, 5);

    }

}
/**
 * 金豆兑换余额例子，如果有错误帮我补充一下：
 * 条件：金豆价格10，
 * 过程：兑换金豆100，用户增加余额1000元 如果有手续费启用1%，用户扣减手续费10元 ，奖池增加10元。
 * 说明：
 * 1不包含支付密码：支付密码接口缓存5分钟，点击兑换之前先验证支付密码服务，所有服务共享支付码
 * 2余额有两条消息事件消息，1是兑换增加余额1000元，2是手续费减少余额10元 奖池有一条时间消息 金豆行情有一条消息改变价格
 */